<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="运营大区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
              <a-tree-select
                v-model="model.sysOrgCode"
                :tree-data="deptTreeDict"
                :replaceFields="replaceFields"
                :show-checked-strategy="SHOW_PARENT"
                search-placeholder="请选择运营大区"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="景点名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入景点名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <!-- <a-input v-model="model.address" placeholder="请输入地址"  ></a-input> -->
              <SearchLocation v-model="model.address"  :lat.sync="model.latitude" :lng.sync="model.longitude"></SearchLocation>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="评分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="score">
              <a-input-number v-model="model.score" placeholder="请输入评分" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="门票价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ticketPrice">
              <a-input-number v-model="model.ticketPrice" placeholder="请输入门票价格" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="营业时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bizTimeRange">
              <a-input v-model="model.bizTimeRange" placeholder="请输入营业时间"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="景点图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="image">
              <j-image-upload :fileSizeLimit="500" text="上传景点图片" v-model="model.image" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="维度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="latitude">
              <a-input v-model="model.latitude" placeholder="请输入维度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="longitude">
              <a-input v-model="model.longitude" placeholder="请输入经度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入地址"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="游玩建议" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="suggestInfo">
              <j-editor  v-model="model.suggestInfo" placeholder="请输入游玩建议"></j-editor>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="景点介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="intro">
              <a-button id="copy-btn" @click="copeIntroText">复制纯文本</a-button>
              <j-editor  v-model="model.intro" placeholder="请输入景点介绍"></j-editor>
            </a-form-model-item>
          </a-col>
          
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { TreeSelect } from 'ant-design-vue';
  const SHOW_PARENT = TreeSelect.SHOW_PARENT;
  const replaceFields =	{children:'children', title:'title', key:'key', value: 'orgCode' };
  import SearchLocation from "@/components/map/dialog/SearchLocation"
  import Clipboard from 'clipboard'
  export default {
    name: 'RentalScenicSpotForm',
    components: {
      SearchLocation
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入景点名称!'},
           ],
        },
        url: {
          add: "/rental/scenicspot/add",
          edit: "/rental/scenicspot/edit",
          queryById: "/rental/scenicspot/queryById",
          queryTreeUrl: "/sys/sysDepart/queryTreeList",
        },
        deptTreeDict:[],
        SHOW_PARENT,
        replaceFields
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.getDeptTreeDict();
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      getDeptTreeDict(){
        getAction(this.url.queryTreeUrl,{}).then((res)=>{
          this.deptTreeDict=res.result;
        }).finally(() => {

        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      async copeIntroText(){
        console.log("copeIntroText:", this.model.intro);
        const tempDiv = document.createElement('div');  
        tempDiv.innerHTML = this.model.intro;  
        const plainText = tempDiv.textContent; 
        // let that = this;
        let clipboard = new Clipboard("#copy-btn", {
          text:(trigger)=> {
            console.log(trigger);
            //返回字符串
            this.$message.success('复制成功');
            return plainText;
          },
        });
        // 复制成功
        clipboard.on("success", (e) => {
          console.log(e, '复制成功');
          clipboard.destroy();
        });
        // //复制失败
        clipboard.on("error", (e) => {
          console.log(e, '复制失败');
          clipboard.destroy();
        });

        
        // console.log("copeIntroText:", plainText);
        // await navigator.clipboard.writeText(this.textToCopy);  
        // 提示用户已复制  
        // alert('文本已复制到剪切板！');  
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>